<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button
      class="post-btn"
       @click="onPost"
    >发布</van-button>
  </div>
</template>

<script>
import { addComment } from '@/api/comment.js'
// import eventBus from '@/utils/eventBus.js'
export default {
  name: 'CommentPost',
  components: {},
  inject: {
    // 孙子组件对象接收注入数据
    articleId: {
      type: [String, Number, Object]
    }
  },
  props: {
    target: [String, Number, Object]
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onPost () {
      // 1.0 发请求
      // await addComment({ target: this.articleId, content: this.message })
      // 得到新的数据
      const { data: { data: { new_obj: comment } } } =
      await addComment({
        target: this.target,
        content: this.message,
        art_id: this.articleId ? this.articleId.toString() : null
      })
      // 2.0 提示
      this.$toast.success('操作成功')
      // 3.0 清空内容
      this.message = ''
      // 4.0关闭弹层
      // this.$emit('close')
      // 通知父组件关闭弹层，评论数加一
      // eventBus.$emit('onPostSuccess', comment)
      // eventBus.$emit('onPostSuccess')
      // this.$emit('onPostSuccess', comment)
      this.$emit('onPostSuccess', comment)
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
